//
//  AlbumListViewController.swift
//  photoTool
//
//  Created by 刘清元 on 2022/11/25.
//  相册

import UIKit
import Photos
class AlbumListViewController: UIViewController {
    //0.扫描相册 1.扫描重复内容
    public var type = 0
    private var  listArr:[AlbumModel] = []{
        didSet{
            mainTable.listArr = listArr
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        PhotoSingle.shared.getAllAlbum { list in
            var albumList = list
            let albumModel = AlbumModel.init()
            albumModel.albumName = "全部"
            albumList.insert(albumModel, at: 0)
            self.listArr = albumList
        }
    }
    lazy var mainTable: UUTableView = {
        let tableView = UUTableView.init(frame: .zero, style: .plain)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.cellForRow { indexPath in
            let model = self.listArr[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
            cell.textLabel?.text = model.albumName
            return cell
        }
        tableView.cellItemDidSelected { indexPath in
            let model = self.listArr[indexPath.row]
            if self.type == 0{
                let alert = UIAlertController.init(title: "提示", message: "是否扫描相册", preferredStyle: .alert)
                let cancelAction = UIAlertAction.init(title: "取消", style: .cancel)
                alert.addAction(cancelAction)
                let oneAction = UIAlertAction.init(title: "不扫描", style: .default) { action in
                    let VC = PhotoListViewController.init()
                    if indexPath.row == 0 {
                        VC.need_scan = false
                        VC.isAlbum = false
                    }else{
                        VC.need_scan = false
                        VC.isAlbum = true
                        VC.albmModel = model
                    }
                    self.navigationController?.pushViewController(VC, animated: true)
                }
                alert.addAction(oneAction)
                let sureAction = UIAlertAction.init(title: "确定", style: .default) { action in
                    let VC = PhotoListViewController.init()
                    if indexPath.row == 0 {
                        VC.need_scan = true
                        VC.isAlbum = false
                    }else{
                        VC.need_scan = true
                        VC.isAlbum = true
                        VC.albmModel = model
                    }
                    self.navigationController?.pushViewController(VC, animated: true)
                }
                alert.addAction(sureAction)
                self.present(alert, animated: true)
            }else{
                let VC = RepeatedPhotoViewController()
                VC.album = model
                self.navigationController?.pushViewController(VC, animated: true)
            }
        }
        return tableView
    }()
}


